export default {
	watch: {
		'list.loading'(val) {
			if (val) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
			} else {
				uni.hideLoading()
			}
		}
	},
	computed: {
		totalPages() {
			if (!this.$util.isEmpty(this.list.total)) {
				return Math.ceil(this.list.total / this.list.limit)
			}
			return 0
		}
	},
	data() {
		return {
			list: {
				data: [],
				page: 1,
				limit: 5,
				loading: true,
				finished: false,
				total: 0
			}
		}
	},
	methods: {
		resetList() {
			this.list = {
				data: [],
				page: 1,
				limit: 5,
				loading: true,
				finished: false,
				total: null
			}
		},
		completes(res, listKey) {
			this.list.loading = false
			if (res.status !== 'success') {
				this.list.finished = true
			}
			this.list.data = this.list.page === 1
				? res.data.list || res.data.data || res.data[listKey] : this.list.data.concat(res.data.list || res.data.data || res.data[listKey])
			this.list.total = res.data.total
			if (this.totalPages <= this.list.page ||
				(!this.$util.isEmpty(res.data.current_page) && res.data.current_page == res.data.last_page)) {
				this.list.finished = true
			} else {
				++this.list.page
			}
		}
	}

}
